﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Google.GData.Apps;

namespace Google.Apps.MMC.Users
    {
    public partial class UserNicknames : CustomControl
        {
        private CustomPropertyPage parent;
        private UserNode user;

        private List<string> nicknames = new List<string>();

        public UserNicknames(UserNode user, CustomPropertyPage parent)
            {
            this.parent = parent;
            this.user = user;
            InitializeComponent();
            NicknameFeed nicknames = GoogleAppsSnapIn.appservice.RetrieveNicknames(user.userentry.Login.UserName);
            if (nicknames.Entries.Count != 0)
                {
                this.nicknamelist.Items.Clear();
                foreach (NicknameEntry nickname in nicknames.Entries)
                    {
                    this.nicknames.Add(nickname.Nickname.Name);
                    this.nicknamelist.Items.Add(nickname.Nickname.Name);
                    }
                }
            }

        private void newnickname_Click(object sender, EventArgs e)
            {
                NewNickname dialog = new NewNickname();
                if (this.parent.ParentSheet.ShowDialog(dialog) == DialogResult.OK)
                    {
                    this.parent.Dirty = true;
                    this.nicknamelist.Items.Add(dialog.nickname.Text);
                    }
            }

        private void delete_Click(object sender, EventArgs e)
            {
                if (this.nicknamelist.SelectedItems.Count > 0)
                    {
                    this.parent.Dirty = true;
                    string[] nicknames = new string[this.nicknamelist.SelectedItems.Count];
                    this.nicknamelist.SelectedItems.CopyTo(nicknames, 0);
                    foreach (string nickname in nicknames)
                        {
                        this.nicknamelist.Items.Remove(nickname);
                        }
                    }
            }

        public override bool Apply()
            {
                if (this.parent.Dirty)
                    {
                    List<string> newnicknames = new List<string>();
                    List<string> deletednicknames = this.nicknames;
                    foreach (string nickname in this.nicknamelist.Items)
                        {
                        if (deletednicknames.Contains(nickname))
                            {
                            deletednicknames.Remove(nickname);
                            }
                        else if (!this.nicknames.Contains(nickname))
                            {
                            newnicknames.Add(nickname);
                            }
                        }

                    foreach (string nickname in newnicknames)
                        {
                        GoogleAppsSnapIn.appservice.CreateNickname(this.user.userentry.Login.UserName, nickname);
                        }
                    foreach (string nickname in deletednicknames)
                        {
                        GoogleAppsSnapIn.appservice.DeleteNickname(nickname);
                        }
                    return true;
                    }
                else
                    {
                    return true;
                    }
            
            }

        public override bool CanApplyChanges()
            {
                return true;
            }


        }
    }
